package com.ct.gd.passport.view.exhibition;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ct.gd.passport.R;
import com.ct.gd.passport.common.constants.Config.Area;
import com.ct.gd.passport.common.constants.UmengVar;
import com.ct.gd.passport.domain.ExhibitionInfo;
import com.ct.gd.passport.view.BundleKeys;
import com.ct.gd.passport.view.LocationFragment;
import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public class ExhibitionInfoFragment extends ListFragment implements LocationFragment.OnLocationChangedListener,
		LoaderCallbacks<List<ExhibitionInfo>> {
	private int									mLevel;
	private SimpleListAdapter<ExhibitionInfo>		mListAdapter;
	private LoaderManager						loaderManager;
	private List<OnStepIntoNextLevelListener>	mOnStepIntoNextLevelListeners	= new ArrayList<ExhibitionInfoFragment.OnStepIntoNextLevelListener>();

	public interface OnStepIntoNextLevelListener {
		void onStepIntoNextLevel(ExhibitionInfo catalog, int level);
	}

	public boolean registerOnStepIntoNextLevelListener(OnStepIntoNextLevelListener listener) {
		return mOnStepIntoNextLevelListeners.add(listener);
	}

	public boolean unregisterOnStepIntoNextLevelListener(OnStepIntoNextLevelListener listener) {
		return mOnStepIntoNextLevelListeners.remove(listener);
	}

	protected final void notifyOnStepIntoNextLevel(ExhibitionInfo catalog) {
		if (null != mOnStepIntoNextLevelListeners) {
			for (int i = 0; i < mOnStepIntoNextLevelListeners.size(); i++) {
				mOnStepIntoNextLevelListeners.get(i).onStepIntoNextLevel(catalog, mLevel);
			}
		}
	}

	public ExhibitionInfoFragment() {
		super();
	}

	/**
	 * @param level start from 0 which is the root level
	 */
	public ExhibitionInfoFragment(int level) {
		this.mLevel = level;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.catalog_list, null);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mListAdapter =  new ExhibitionInfoListAdapter(getActivity(), null);
		setListAdapter(mListAdapter);
		getListView().setItemsCanFocus(false);
	}

	/**
	 * start from 0 which is the root level
	 */
	public int getLevel() {
		return mLevel;
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		ExhibitionInfo ExhibitionInfo = (ExhibitionInfo)l.getItemAtPosition(position);
		MobclickAgent.onEvent(getActivity(), UmengVar.TRAVEL_EXHIBITION_DETAIL);
		// enter detail
		Intent intent = new Intent(this.getActivity(), ExhibitionDetailActivity.class);
		intent.putExtra(BundleKeys.CATALOG_ID, ExhibitionInfo.getId());
		startActivity(intent);
	}
	public class ExhibitionInfoListAdapter extends SimpleListAdapter<ExhibitionInfo> {

		private class ViewHolder {
			TextView	tvName;
		}

		public ExhibitionInfoListAdapter(Context context, List<ExhibitionInfo> data) {
			super(context, data);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.root_catalog_list_item, null);
				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.tv_root_catalog_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(getData().get(position).getName());
			return convertView;
		}

	}

	protected void loadCatalog() {
		load(getCatalogLoader());
	}

	protected ExhibitionLoader getCatalogLoader() {
		loaderManager = getActivity().getSupportLoaderManager();
		Loader<List<ExhibitionInfo>> loader = loaderManager.getLoader(mLevel);
		if (null == loader) loader = loaderManager.initLoader(mLevel, null, this);
		return (ExhibitionLoader) loader;
	}

	protected void load(ExhibitionLoader loader) {
		loader.reset();
		loader.startLoading();
	}

	@Override
	public void onLocationChanged(Area area) {
		loadCatalog();
	}


	@Override
	public Loader<List<ExhibitionInfo>> onCreateLoader(int id, Bundle args) {
		return new ExhibitionLoader(getActivity(), getListView());
	}

	@Override
	public void onLoadFinished(Loader<List<ExhibitionInfo>> loader, List<ExhibitionInfo> data) {
		mListAdapter.setData(data);
		mListAdapter.notifyDataSetChanged();
		getListView().clearChoices();
	}

	@Override
	public void onLoaderReset(Loader<List<ExhibitionInfo>> loader) {
	}

}
